<template>
	<view id="selector-area">
		<uni-collapse v-if="areaList.length !== 1" accordion v-model="currentTab" @change="open">
			<uni-collapse-item v-for="(item,index) in areaList" :name="item.id" :key="index" :title="item.areaName">
				<template v-slot:title>
					<view class="v-selector-title">
						<image class="img" src="https://bc-staticfiles.oss-cn-zhangjiakou.aliyuncs.com/wechat/layer2.png"></image>
						{{item.areaName}}
					</view>
				</template>
				<u-cell isLink icon="https://bc-staticfiles.oss-cn-zhangjiakou.aliyuncs.com/wechat/layer3.png" :title="item.areaName" @click="backPage(item.id, item.areaName)"></u-cell>
				<u-cell isLink icon="https://bc-staticfiles.oss-cn-zhangjiakou.aliyuncs.com/wechat/layer4.png" v-for="(son, i) in item.children" :title="son.areaName" :key="i" @click="backPage(son.id, son.areaName)"></u-cell>
			</uni-collapse-item>
		</uni-collapse>
		<u-cell v-else isLink icon="https://bc-staticfiles.oss-cn-zhangjiakou.aliyuncs.com/wechat/layer3.png" :title="areaList[0].areaName" @click="backPage(areaList[0].id, areaList[0].areaName)"></u-cell>
	</view>
</template>

<script>
	import { getAreaList } from '@/api/global'
	export default {
		data() {
			return {
				currentTab: '',
				areaList: [],
				queryParam: {}
			}
		},
		onLoad(option) {
			for (const k in option) {
				this.queryParam[k] = option[k]
			}
		},
		onShow() {
			this.getList()
		},
		methods: {
			open(e) {
				// console.log(e)
			},
			getList() {
				uni.showLoading({
					title: '数据加载中',
					mask: true
				})
				getAreaList().then(res => {
					this.areaList = res.data.data
					uni.hideLoading()
				})
			},
			backPage (id, name) {
				this.queryParam.areaId = id
				this.queryParam.areaName = name
				const url = this.queryParam.url
				let params = ''
				delete this.queryParam.url
				for (const k in this.queryParam) {
					params += `${k}=${this.queryParam[k]}&`
				}
				uni.redirectTo({
					url: '/' + url + '?' + params
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	#selector-area {
		.v-selector-title {
			display: flex;
			justify-content: start;
			align-items: center;
			padding: 20upx 0 20upx 20upx;
			font-size: 32upx;
			.img {
				width: 40upx;
				height: 40upx;
				margin-right: 10upx;
			}
		}
	}
</style>
